package com.rentalHome.controller;

import com.rentalHome.domain.User;
import com.rentalHome.repository.UserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Created by leeXyan on 2017/05/16 14:37
 */
@RestController
@RequestMapping("/sys")
public class SettingController {
    @Autowired
    private UserRepository userRepository;

    @PostMapping("/add")
    public ResponseEntity settingAdd(@RequestParam("username") String username,
                                     @RequestParam("electPrice") Double electPrice,
                                     @RequestParam("waterPrice") Double waterPrice,
                                     @RequestParam("netPrice") Double netPrice) {
        User user=userRepository.findOne(getId(userRepository.findByUsername(username)));
        user.setElectPrice(electPrice);
        user.setWaterPrice(waterPrice);
        user.setNetPrice(netPrice);
        return ResponseEntity.ok(userRepository.save(user));
    }

    public Long getId(List<User> users) {
        return users.stream().filter(user -> !StringUtils.isEmpty(user.toString())).findFirst().get().getId();
    }
}
